<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2019/12/18
 * Time: 10:23
 *
 */

require_once '../../lib/start.php';

$config = [
    '出版社' => 'publish',
    'ISBN' => 'isbn',
    '出版时间' => 'publish_date',
];

$path = $ROOT_PATH . 'data/jp_i.log';
$i = file_get_contents($path) ? file_get_contents($path) : 70010000000026;

for (; $i < 70010000000027; $i++) {
    try{
        $item = get_game($i);
    }catch (\Exception $e){
        var_dump($e->getMessage());
    }
    if (empty($item)) {
        continue;
    }
    var_dump($item);
    /*if (!$db->get('switch_country_game', ['isbn' => $item['isbn']])) {
        $item['country'] = 'jp';
        $item['currency'] = 'JPY';
        $item['crawl_no'] = $i;
        $db->insert('switch_country_game', $item);
    }*/
    var_dump("{$i}:{$item['title']}");

    file_put_contents($path, $i);
}

/**
 * @param $book_num
 * @return array
 * @throws Exception
 */
function get_game($book_num)
{
    global $client, $config;
    $book_insert = [];
    try {
        $client->request('GET', "https://ec.nintendo.com/api/JP/ja/guest_prices?ns_uids=70010000000026");
        $responseContentPrice = $client->getResponse()->getContent();
        var_dump($responseContentPrice);
        $responseContentPriceArr = json_decode($responseContentPrice,true);
        var_dump($responseContentPriceArr);
        $insert['price'] = $responseContentPriceArr[0];
        var_dump($insert['price']);exit;
        $book_insert['name'] = trim($crawler->filter('#root > div > div > article > section > div.o_c-page-title > h1')->text());
        //$book_insert['brief'] = trim($crawler->filter('#accordion1 > div > div')->text());
        //$book_insert['price'] = trim($crawler->filter('#description > section.o_p-product-detail-frame.o_p-product-detail-content.is-carousel > section.o_p-product-detail-content__right > div > div > div > div.o_p-product-detail__price-wrap > div.o_p-product-detail__price > div.o_p-product-detail__price--price')->attr('src'));
        //$book_insert['discount'] = trim($crawler->filter('#spec-n1 > img')->attr('src'));

        /*$books_infos = $crawler->filter('#parameter2 li')->each(function ($node) {
            return $node->text();
        });*/


        $book_insert['create_time'] = $book_insert['update_time'] = date('Y-m-d H:i:s');
        return $book_insert;
    } catch (Exception $e) {
        throw new \Exception($e->getMessage());
    }

}